package com.leyou.item.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YYP
 * @date 2020/12/27
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;



    //通过控制器注入
    public CategoryController(CategoryService categoryService){
        this.categoryService = categoryService;

    }


    /**
     * 根据id查询分类
     * @param id 分类id
     * @return 分类对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id) {

        CategoryDTO categoryDTO = categoryService.queryCategoryById(id);

        //return ResponseEntity.ok(new CategoryDTO(categoryService.getById(id)));
        return ResponseEntity.ok(categoryDTO);
    }

    /**
     * 根据分类id集合查询对应分类集合，但是前端参数是字符串
     * @param ids ids原理：本质上是一个字符串，但我们需要将这个字符串理解为数组，mvc进行了封装，于是前端穿一个字符串，我们可以用数组接收
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids") List<Long> ids){
        /*List<Category> categories = categoryService.listByIds(ids);
        List<CategoryDTO> categoryDTOS = CategoryDTO.convertEntityList(categories);*/
        List<CategoryDTO> categoryDTOS = categoryService.listCategoryByIds(ids);
        return ResponseEntity.ok(categoryDTOS);
    }


    /**
     * 根据品牌id查询对应的分类集合
     * @param brandId
     * @return
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") Long brandId){
        return ResponseEntity.ok(categoryService.queryCategoryByBrandId(brandId));
    }


    /**
     * 根据父类目id查询子类目的集合
     * @param parentId 父类目的id
     * @return 子类目的集合
     * select * from tb_category where parent_id =#{parentId}
     * 将entity==》dto
     */
    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid") Long parentId){
        /*QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentId);
        List<Category> categoryList = categoryService.list(wrapper);
        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryList));*/
        return ResponseEntity.ok(categoryService.listCategoryByPid(parentId));
    }

}
